﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace webnhom.backend
{
    public partial class ListArticle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCategory();
                LoadData(int.Parse(dropSate.SelectedValue),txtSearch.Text,droPhanLoai.SelectedValue);
            }
        }
        public void LoadCategory()
        {
            //ListItem item = new ListItem();
            //item.Text = "-----Tất cả-----";
            //item.Value = "-1";
            //item.Selected = true;
            //dropCategory.DataSource = BLL.CategoryHelper.DS_Category_PhanLoai(2);
            //dropCategory.DataTextField = "CategoryName";
            //dropCategory.DataValueField = "CategoryId";
            //dropCategory.DataBind();
            //dropCategory.Items.Add(item);
        }
        private void LoadData(int State, string txtSearch,string PhanLoai)
        {
            PagedDataSource objPDS = new PagedDataSource();
            objPDS.AllowPaging = true;
            objPDS.PageSize = 10;
            objPDS.DataSource = BLL.ArticleHelper.List_Article_Admin(State,txtSearch,PhanLoai);
            if (objPDS.Count > 0)
            {

                rptArticle.Visible = true;
                lnkNextPage.Visible = true;
                lnkPreviousPage.Visible = true;
                if (CurrentPage == objPDS.PageCount - 1)
                    lnkNextPage.Visible = false;
                if (CurrentPage == 0)
                    lnkPreviousPage.Visible = false;
                objPDS.CurrentPageIndex = CurrentPage;
                lblCurrentPage.Text = "Trang: " + (CurrentPage + 1).ToString() + " / "
                                               + objPDS.PageCount.ToString();


                lnkPreviousPage.Enabled = !objPDS.IsFirstPage;
                lnkNextPage.Enabled = !objPDS.IsLastPage;
                rptArticle.DataSource = objPDS;
                rptArticle.DataBind();
            }
            else
            {
                rptArticle.Visible = false;
                lnkNextPage.Visible = false;
                lnkPreviousPage.Visible = false;
            }
        }

        #region LoadSearch


        public int CurrentPage
        {
            get
            {
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;
                else
                    return (int)o;
            }
            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }

        #endregion
        #region LnkPreviousPage_click
        protected void lnkPreviousPage_Click(object sender, EventArgs e)
        { //Set viewstate variable to the previous page
            CurrentPage -= 1;
            LoadData( int.Parse(dropSate.SelectedValue), txtSearch.Text,droPhanLoai.SelectedValue);
        }
        #endregion
        #region lnkNextPage_Click
        protected void lnkNextPage_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            //Reload control
            LoadData(int.Parse(dropSate.SelectedValue), txtSearch.Text, droPhanLoai.SelectedValue);
        }
        #endregion
        protected void btSearch_Click(object sender, EventArgs e)
        {
            CurrentPage = 0;
            LoadData(int.Parse(dropSate.SelectedValue), txtSearch.Text, droPhanLoai.SelectedValue);
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptArticle.Items)
            {
                HiddenField hiArticleId = item.FindControl("hiArticleId") as HiddenField;
                CheckBox CheckBox1 = item.FindControl("CheckBox1") as CheckBox;
                if (CheckBox1.Checked == true)
                {
                    BLL.ArticleHelper.Delete_Article(int.Parse(hiArticleId.Value));
                }
            }
            CurrentPage = 0;
            LoadData(int.Parse(dropSate.SelectedValue), txtSearch.Text, droPhanLoai.SelectedValue);
        }

        protected void btDuyetBai_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptArticle.Items)
            {
                HiddenField hiArticleId = item.FindControl("hiArticleId") as HiddenField;
                CheckBox CheckBox1 = item.FindControl("CheckBox1") as CheckBox;
                if (CheckBox1.Checked == true)
                {
                    BLL.ArticleHelper.Duyet_Huy_Bai(int.Parse(hiArticleId.Value),0);
                }
            }
            CurrentPage = 0;
            LoadData(int.Parse(dropSate.SelectedValue), txtSearch.Text, droPhanLoai.SelectedValue);
        }

        protected void rptArticle_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int articleid = int.Parse(e.CommandArgument.ToString());
                DAL.tbl_Article article = BLL.ArticleHelper.Get_ArticleId(articleid);
                Response.Redirect("/EditArticle"+articleid+"/"+article.URL+".html");
            }
            if (e.CommandName == "Delete")
            {
                int articleid = int.Parse(e.CommandArgument.ToString());
                BLL.ArticleHelper.Delete_Article(articleid);
                CurrentPage = 0;
                LoadData(int.Parse(dropSate.SelectedValue), txtSearch.Text, droPhanLoai.SelectedValue);
            }
        }

        protected void btHuyDuyet_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptArticle.Items)
            {
                HiddenField hiArticleId = item.FindControl("hiArticleId") as HiddenField;
                CheckBox CheckBox1 = item.FindControl("CheckBox1") as CheckBox;
                if (CheckBox1.Checked == true)
                {
                    BLL.ArticleHelper.Duyet_Huy_Bai(int.Parse(hiArticleId.Value),1);
                }
            }
            CurrentPage = 0;
            LoadData(int.Parse(dropSate.SelectedValue), txtSearch.Text, droPhanLoai.SelectedValue);
        }
       
        
    }
}